package com.th.supcom.pds.drugreturn.client.widget;

import java.util.List;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.KeyNav;
import com.extjs.gxt.ui.client.widget.Component;
import com.th.supcom.pds.common.client.commom.FocusEvents;
import com.th.supcom.pds.common.constant.FocusConstants;
import com.th.supcom.pds.common.constant.KeysConstants;
import com.th.supcom.pds.common.utils.ClientUtils;
import com.th.supcom.pds.sortingdrug.client.model.PdsPrescDetailInfoModel;

/**
 * 门诊退药时间监听
 * 
 * @author yzhang
 */
public class RefundApplicationListener
{
    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";
    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";
    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    private DrugReturnAndRefundFields m_drugReturnAndRefundFields;

    private FocusEvents m_focusEvents;

    /** 需要监听的全局面板. */
    private Component m_keyNavComponent;

    private RefundApplicationAction m_refundApplicationAction;

    private RefundApplicationButtons m_refundApplicationButtons;

    private DrugReturnAndRefundPrescPanel m_drugReturnAndRefundPrescPanel;

    private void addClearKeyListener ()
    {
        m_refundApplicationButtons.getClearBtn ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {
            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                m_refundApplicationAction.clear (true);
            }
        });
    }

    /**
     * 监听快捷键
     */
    private void addEnterKeyListener ()
    {
        new KeyNav <ComponentEvent> (m_keyNavComponent)
        {
            @Override
            public void onKeyPress (final ComponentEvent ce)
            {
                final int keyCode = ce.getKeyCode ();

                if (keyCode == KeysConstants.VK_ENTER)
                {
                    executEnterEvent ();
                }

                if (ce.isAltKey ())
                {
                    executAltEvent (keyCode);
                }

            }
        };
    }

    /**
     * 监听焦点事件
     */
    private void addFocusListeners ()
    {
        m_focusEvents = new FocusEvents ();
        m_focusEvents.add (FocusConstants.REFUND_APPL_PRESC_NO, m_drugReturnAndRefundFields.getPrescNoField ());
        // TODO
        // m_focusEvents.add (FocusConstants.REFUND_APPL_MEDICARECARD, null);
        ClientUtils.getFocusChange (m_drugReturnAndRefundFields.getPrescNoField (), m_refundApplicationButtons.getRefundApplicationBtn (),
                                    m_refundApplicationButtons.getQueryBtn ());
        ClientUtils.getFocusChange (m_refundApplicationButtons.getQueryBtn (), m_refundApplicationButtons.getClearBtn (), m_refundApplicationButtons.getRefundApplicationBtn ());
        ClientUtils.getFocusChange (m_refundApplicationButtons.getRefundApplicationBtn (), null, m_refundApplicationButtons.getClearBtn ());
    }

    /**
     * 添加查询事件，但点击查询按钮时，根据输入处方号进行查询获得处方明细信息
     */
    private void addQueryByClickButtonListener ()
    {
        m_refundApplicationButtons.getQueryBtn ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {
            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                m_refundApplicationAction.findPrescDetailByPresc (m_refundApplicationButtons.getRefundApplicationBtn (), m_drugReturnAndRefundFields.getPrescNoField ());
            }
        });
    }

    /**
     * 监听申请退费按钮，单击申请退费执行退费操作
     */
    private void addRefundApplicationListener ()
    {
        m_refundApplicationButtons.getRefundApplicationBtn ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {
            @Override
            public void componentSelected (final ButtonEvent ce)
            {
                m_refundApplicationAction.refundApplicate (m_drugReturnAndRefundFields.getPrescNoField (), m_drugReturnAndRefundFields.getPrescNoField ());
            }
        });
        m_drugReturnAndRefundPrescPanel.getCheckmodel ().addSelectionChangedListener (new SelectionChangedListener <PdsPrescDetailInfoModel> ()
        {
            @Override
            public void selectionChanged (SelectionChangedEvent <PdsPrescDetailInfoModel> se)
            {
                if (se == null)
                {
                    return;
                }
                List <PdsPrescDetailInfoModel> model = se.getSelection ();
                m_refundApplicationAction.doSelectedChange (model);
            }
        });
    }

    /**
     * 执行Alt+?快捷键
     */
    private void executAltEvent (final int keyCode)
    {
        if (keyCode == KeysConstants.VK_Q)
        {
            m_refundApplicationAction.findPrescDetailByPresc (m_refundApplicationButtons.getRefundApplicationBtn (), m_focusEvents.getFocusComponent ());
            return;
        }

        if (keyCode == KeysConstants.VK_O)
        {
            m_refundApplicationAction.refundApplicate (m_drugReturnAndRefundFields.getPrescNoField (), m_refundApplicationButtons.getRefundApplicationBtn ());
            return;
        }
        if (keyCode == KeysConstants.VK_R)
        {
            m_refundApplicationAction.clear (true);
            return;
        }
    }

    /**
     * 执行回车事件
     */
    private void executEnterEvent ()
    {
        if (m_focusEvents.getFocusStatus (FocusConstants.REFUND_APPL_PRESC_NO))
        {
            m_refundApplicationAction.findPrescDetailByPresc (m_refundApplicationButtons.getRefundApplicationBtn (), m_focusEvents.getFocusComponent ());
        }
    }

    // public void setFocusEvents (FocusEvents focusEvents)
    // {
    // m_focusEvents = focusEvents;
    // }

    /**
     * 初始化所有的Listener
     */
    public void initListeners ()
    {
        addQueryByClickButtonListener ();
        addRefundApplicationListener ();
        addClearKeyListener ();
        addFocusListeners ();
        addEnterKeyListener ();
    }

    public void setDrugReturnAndRefundFields (final DrugReturnAndRefundFields drugReturnAndRefundFields)
    {
        m_drugReturnAndRefundFields = drugReturnAndRefundFields;
    }

    public void setKeyNavComponent (final Component keyNavComponent)
    {
        m_keyNavComponent = keyNavComponent;
    }

    public void setRefundApplicationAction (final RefundApplicationAction refundApplicationAction)
    {
        m_refundApplicationAction = refundApplicationAction;
    }

    public void setRefundApplicationButtons (final RefundApplicationButtons refundApplicationButtons)
    {
        m_refundApplicationButtons = refundApplicationButtons;
    }

    public void setDrugReturnAndRefundPrescPanel (DrugReturnAndRefundPrescPanel drugReturnAndRefundPrescPanel)
    {
        m_drugReturnAndRefundPrescPanel = drugReturnAndRefundPrescPanel;
    }

}
